package org.smartdevelop.reviewboard.action;

import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.DefaultListModel;
import javax.swing.text.JTextComponent;
import org.netbeans.modules.versioning.util.UndoRedoSupport;
import org.smartdevelop.reviewboard.client.response.ReviewRequest;
import org.smartdevelop.reviewboard.diff.VcsFile;
import org.smartdevelop.reviewboard.options.RBConfigurationPanel;

/**
 * Panel to contain the information about each review request to be created.
 *
 * @author Pragalathan M
 */
public class ReviewRequestPanel extends javax.swing.JPanel {

    private List<VcsFile> files;
    private ReviewRequest reviewRequest;
    private String diff;
    private UndoRedoSupport um1;
    private UndoRedoSupport um2;

    /**
     * Creates new form ReviewRequestPanel
     */
    public ReviewRequestPanel(List<VcsFile> files) {
        this.files = files;
        initComponents();
        Preferences node = Preferences.userNodeForPackage(RBConfigurationPanel.class);
        summaryTextField.setText(node.get("summary", ""));
        descriptionTextPane.setText(node.get("description", ""));
        groupTextField.setText(node.get("group", ""));
        reviewerTextField.setText(node.get("reviewer", ""));

        DefaultListModel model = new DefaultListModel();
        for (VcsFile file : files) {
            model.addElement(file.getFile());
        }
        changedFilesList.setModel(model);
    }

    @Override
    public void addNotify() {
        super.addNotify();
        um1 = UndoRedoSupport.register(summaryTextField);
        um2 = UndoRedoSupport.register(descriptionTextPane);
    }

    @Override
    public void removeNotify() {
        if (um1 != null) {
            um1.unregister();
            um1 = null;
        }
        if (um2 != null) {
            um2.unregister();
            um2 = null;
        }
        super.removeNotify();
    }

    public void setDiff(String diff) {
        this.diff = diff;
    }

    public void setReviewRequest(ReviewRequest reviewRequest) {
        this.reviewRequest = reviewRequest;
    }

    public String getDiff() {
        return diff;
    }

    public ReviewRequest getReviewRequest() {
        return reviewRequest;
    }

    public String getSummary() {
        return summaryTextField.getText();
    }

    public String getReviewer() {
        return reviewerTextField.getText();
    }

    public String getGroup() {
        return groupTextField.getText();
    }

    public String getDescription() {
        return descriptionTextPane.getText();
    }

    public JTextComponent getSummaryComponent() {
        return summaryTextField;
    }

    public JTextComponent getReviewerComponent() {
        return reviewerTextField;
    }

    public JTextComponent getGroupComponent() {
        return groupTextField;
    }

    public JTextComponent getDescriptionComponent() {
        return descriptionTextPane;
    }

    public List<VcsFile> getFiles() {
        return files;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextPane = new javax.swing.JTextPane();
        reviewerTextField = new javax.swing.JTextField();
        groupTextField = new javax.swing.JTextField();
        summaryTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        changedFilesList = new javax.swing.JList();

        setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setViewportView(descriptionTextPane);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 50;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        add(jScrollPane1, gridBagConstraints);

        reviewerTextField.setText(org.openide.util.NbBundle.getMessage(ReviewRequestPanel.class, "ReviewRequestPanel.reviewerTextField.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        add(reviewerTextField, gridBagConstraints);

        groupTextField.setText(org.openide.util.NbBundle.getMessage(ReviewRequestPanel.class, "ReviewRequestPanel.groupTextField.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        add(groupTextField, gridBagConstraints);

        summaryTextField.setText(org.openide.util.NbBundle.getMessage(ReviewRequestPanel.class, "ReviewRequestPanel.summaryTextField.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        add(summaryTextField, gridBagConstraints);

        jLabel3.setText(org.openide.util.NbBundle.getMessage(ReviewRequestPanel.class, "ReviewRequestPanel.jLabel3.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabel3, gridBagConstraints);

        jLabel2.setText(org.openide.util.NbBundle.getMessage(ReviewRequestPanel.class, "ReviewRequestPanel.jLabel2.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabel2, gridBagConstraints);

        jLabel1.setText(org.openide.util.NbBundle.getMessage(ReviewRequestPanel.class, "ReviewRequestPanel.jLabel1.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabel1, gridBagConstraints);

        jLabel4.setText(org.openide.util.NbBundle.getMessage(ReviewRequestPanel.class, "ReviewRequestPanel.jLabel4.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabel4, gridBagConstraints);

        jLabel5.setText(org.openide.util.NbBundle.getMessage(ReviewRequestPanel.class, "ReviewRequestPanel.jLabel5.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabel5, gridBagConstraints);

        jScrollPane3.setViewportView(changedFilesList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        add(jScrollPane3, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList changedFilesList;
    private javax.swing.JTextPane descriptionTextPane;
    private javax.swing.JTextField groupTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField reviewerTextField;
    private javax.swing.JTextField summaryTextField;
    // End of variables declaration//GEN-END:variables
}
